package main.api;

import main.RequestHelper;
import main.ResponseHelper;
import main.database.AccountHelper;
import main.database.NotifyHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class NotifyApi {
    @PostMapping("/api/sendnotify")
    public ResponseEntity<?> sendNotify(HttpServletRequest request) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        boolean isAdmin = AccountHelper.isAdminFromUUID(uuid);

        String result;
        if (isAdmin) {
            String message = null;
            Map<String, String[]> map = request.getParameterMap();
            if (map.containsKey("message")) {
                message = map.get("message")[0];
            }

            if (message != null && !message.isEmpty()) {
                NotifyHelper.addNotifyToAll(message);
                result = ResponseHelper.createBasicJsonObject(200, "ok").toString();
            }
            else {
                result = ResponseHelper.createBasicJsonObject(404, "沒有相應的通知內容").toString();
            }
        }
        else {
            result = ResponseHelper.createBasicJsonObject(404, "不是認證的用戶，沒有登録?").toString();
        }

        return ResponseEntity.ok(result);
    }

    @PostMapping("/api/deletenotify")
    public ResponseEntity<?> deleteNotify(HttpServletRequest request) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        boolean isLogin = AccountHelper.isExistFromUUID(uuid);

        String id = null;
        Map<String, String[]> map = request.getParameterMap();
        if (map.containsKey("id")) {
            id = map.get("id")[0];
        }

        String result;
        if (isLogin) {
            if (id == null || id.isEmpty()) {
                result = ResponseHelper.createBasicJsonObject(404, "找不到对应的消息ID").toString();
            }
            else {
                NotifyHelper.deleteNotify(id);
                result = ResponseHelper.createBasicJsonObject(200, "ok").toString();
            }
        }
        else {
            result = ResponseHelper.createBasicJsonObject(404, "没有登录").toString();
        }

        return ResponseEntity.ok(result);
    }

    @PostMapping("/api/readnotify")
    public ResponseEntity<?> readNotify(HttpServletRequest request) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        boolean isLogin = AccountHelper.isExistFromUUID(uuid);

        String id = null;
        Map<String, String[]> map = request.getParameterMap();
        if (map.containsKey("id")) {
            id = map.get("id")[0];
        }

        String result = ResponseHelper.createBasicJsonObject(404, "error").toString();
        if (isLogin) {
            if (id != null && !id.isEmpty()) {
                NotifyHelper.readNotify(id);
                result = ResponseHelper.createBasicJsonObject(200, "ok").toString();
            }
        }

        return ResponseEntity.ok(result);
    }
}
